/* eslint-disable */
import moment from 'moment'

const code = {
  create() {
    const str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    const number = this.createNumber()
    const code = this.generateCode(number, str)
    return code
  },
  createNumber() {
    // 获取当前时间戳
    let CurrentTimestamp = moment().valueOf()
    // 获取当前0点时间戳
    let ZeroTimeStamp = new Date(new Date().toLocaleDateString()).getTime()
    let time = parseInt((CurrentTimestamp - ZeroTimeStamp) / 1000)
    time = time * 10000
    // 随机生成1-99
    let randomNum = this.randomNum(1, 99) * 100
    let number = time + randomNum + 99
    return number
  },
  generateCode (number, str) {
    let StringBuilder = ''
    const baseArray = [1, 36, 1296, 46656, 1679616, 60466176]
    for (let i = 5; i >= 0; i--) {
      const base = baseArray[i];
      const index = Number(number / base);
      number = number % base;
      StringBuilder += str.charAt(index);
    }
    return StringBuilder
  },
  randomNum(minNum, maxNum) {
    switch (arguments.length) {
      case 1:
        return parseInt(Math.random() * minNum + 1, 10);
        break;
      case 2:
        return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
        break;
      default:
        return 0;
        break;
    }
  },
  check (code) {
    code = code.toUpperCase();
    let number = Number(this.decodeCode(code));
    this.checkNum(number);
  },
  decodeCode (code) {
    const str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    let baseArray = [1, 36, 1296, 46656, 1679616, 60466176];
    let number = 0;
    for (let i = 0; i < 6; i++) {
        let ch = code.charAt(i);
        let res = str.indexOf(ch);
        let base = baseArray[5-i];
        number += (base* res)
    }
    return number;
  },
  checkNum (number) {
    if(number >= 865000000){
        console.log("超上界,非法");
    }
    let num = number % 100;
    console.log("network=" + num);
    if(num == 0){
      console.log("在线");
    } else if(num == 99){
      console.log("离线");
    } else {
      console.log("非法");
    }
  }
}

export default code
